<?php

namespace Modules\Manager\Http\Controllers;

use App\Helpers\ResponseData;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Carbon;
use Modules\Manager\Http\Requests\RoleRequest;
use Spatie\Permission\Models\Role;
class RoleController extends Controller
{
    use ResponseData;
    public function list()
    {
        $role = Role::query()
            ->select('id','zh_name','name','created_at')
            ->get();
        foreach ($role as $k=>$v){
            $role[$k]['created_at1']= Carbon::parse($v->created_at)->format('Y-m-d H:i:s');//date("Y-m-d H:i:s",$v->created_at);//$date->format($v['created_at'] ?: 'Y-m-d H:i:s');
        }
        return $this->success($role);
    }

    public function store(RoleRequest $request)
    {
        $data = $request->all();
        $data['guard_name'] = 'sanctum';
        $role = Role::create($data);
        return $this->success($role);
    }

    public function edit(RoleRequest $request)
    {
        $data = Role::query()
            ->select('id','zh_name','name')
            ->find($request->id);
        return $this->success($data);
    }

    public function update(Request $request)
    {
        $data = $data = Role::query()->find($request->id);
        $data->update($request->all());
        return $this->success('更新成功');
    }
    public function destory(Request $request)
    {
        $data = $data = Role::query()->find($request->id);
        $data->delete();
        return $this->success('删除成功');
    }
}
